define([
	'selectModify',
	'layer',
	'../../Shop/module/STH_main.js',
	'checkAll',
	'iCheck',
	'imgCenter'
], function(selectModify,layer,main){
	
	return {

		//商品图片居中函数
		goodsImgCenter:function(thisPanel,thisEdit,key){
			$(thisPanel).find(".tech-list.style-small .tech-img, .tech-list.style-big .tech-img").each(function(){
	            $(this).height($(this).parents("li").width());
	        });
	        $(thisPanel).find(".tech-list.style-list .tech-img").each(function(){
	            $(this).height(80);
	        });
	        $(thisPanel).find('.tech-img').imgCenter();
		},
	
	    //加入模块
	    technicianAddM:function(con){
	    	
	    	//去掉重复函数
	    	Array.prototype.deleteEle=function(id){
				    var arr=this,om={},newArr=[],im,nm,idlist=[];
				    for( im=0;im<arr.length;im++){
				        nm=arr[im]+typeof(arr[im]);
				        if(typeof(om[nm])==="undefined"){
				            newArr[newArr.length]=arr[im]
				            idlist.push(arr[im]);
				            om[nm]=1;
				        }
				    }
				    return {'newArr':newArr,'idlist':idlist};
			}
	    	
	    	var _this=this,
	    	    key=con.key,
	    	    prop=con.prop,
	    	    order = con.order,
	    	    item_list=con.item_list,
	    	    level_name=prop.level_name,
	    	    size=prop.size,
	    	    level_id=prop.level_id,
	    	    
	    	    newPanel=main.getPanel("technician"),	                        
	            newEdit=main.getEdit("technician");
			
			//右侧edit属性
			$(newEdit).attr("key",key);
			$(newEdit).find("input[name='size'][value='"+size+"']").attr("checked",'checked');

			var newOption="<option value='' more_url='' level_name=''>请选择</option>",
				grouplen=technician_level!=""?technician_level.length:undefined,
				itempd=false;

			if(grouplen){	
				for(var i=0;i<grouplen; i++){
					newOption+="<option value='"+technician_level[i].level_id+"' more_url='"+technician_level[i].more_url+"' level_name='"+technician_level[i].level_name+"'>"+technician_level[i].level_name+"</option>";
				}
			}
			$(newEdit).find("select[name='choicetech']").append(newOption);
			$(newEdit).find("select[name='choicetech']").val(level_id);
		
			$(newEdit).css("display","none");
			
			
			//左侧panel属性
			$(newPanel).attr("order",order);
		    $(newPanel).attr("key",key);
		    $(newPanel).find(".tech-list").attr("class","tech-list style-"+size);
		    
		    
		    
	        //插入panel(延时顺序重定位)
		    var edLength=$(".hb_box .panel-comp").length;
			if($(".hb_box .panel-comp").length<1 || order>parseInt($(".hb_box .panel-comp").eq(edLength-1).attr("order"))){
			        	$(".hb_box").append(newPanel);
			}else if(order < parseInt($(".hb_box .panel-comp").eq(0).attr("order"))){
			       		$(".hb_box").prepend(newPanel);
			}else{
			       	for(var r=0; r<edLength; r++){
			       		if(order > parseInt($(".hb_box .panel-comp").eq(r).attr("order")) && order < parseInt($(".hb_box .panel-comp").eq(r+1).attr("order"))){
			       				$(".hb_box .panel-comp").eq(r).after(newPanel);
			       				break;
			       		}	       			
			       	}
			}
			    
			//插入edit
	        $(".edit-panel").append(newEdit);


			//人员循环再拉取
			var newPanelLi=$(newPanel).find(".moban").html(),
		        newEditLi=$(newEdit).find(".moban").html(),
		        idlist=item_list.deleteEle("id").idlist,
		        technicianIdStr=idlist.join("|");
			
				$.ajax({
					type: 'post',
					url: '/ShopMain/GetServiceUsersDetail',
					data: {"technicianIdStr":technicianIdStr},
					contentType: "application/x-www-form-urlencoded",
					dataType: "json",
					success: function(json) {
						// console.log(json);
						if(json.success){
							var item_list=json.data,
							    listLng=item_list.length;
							console.log(item_list);    
							g_data[key].item_list=[];//重置列表
							
							if(listLng>0){
								$(newEdit).find(".tech-list-show ul").html("");
								$(newPanel).find(".tech-list ul").html("");
						        for(var d=0;d<listLng;d++){
									    	
									    var newobj={
									    		"id":item_list[d]['id'],
									    		"photo_url":item_list[d]['photo_url'],
									    		"nickname": item_list[d]['name'],
									    		"level":item_list[d]['level'],
									    		"sale":item_list[d]['sale']
									    	},

							        		nPLi=main.formatTemplate(newobj,newPanelLi),
							        	    nELi=main.formatTemplate(newobj,newEditLi);
							        	    
							        	    
							        	$(newPanel).find(".tech-list ul").append(nPLi);
							        	$(newEdit).find(".tech-list-show ul").append(nELi);
							        	g_data[key].item_list[d]=item_list[d]['id'];
							        	
							        	//图片居中
							        	_this.goodsImgCenter(newPanel,newEdit,key);
						        	//}
		                           
						        }
					        }
					        
					        
					        //调用本模块的操作  
				            _this.technicianActive(newPanel,newEdit,key);							
							
						}else{
							alert(json.msg);
						}
					},
					error: function(mes) {
						alert("网络不太给力哦！");
						console.log("远程请求获取服务人员详情失败，请联络管理员。");
					}
				});
	                        
	    },
	
		//得到当前分组的index
		getGroupIndex:function (obj,id){
			var index=0;
			for(var i=0;i<obj.length;i++){
					if(id==obj[i].group_id){
						index=i;
						break;
					}	
			}
			return index;
		},
	
	
        //常规操作
        technicianActive:function(thisPanel,thisEdit,key){
        	var _this=this;
        	
        	//如果新拖入模块
        	if(typeof(g_data[key].prop.size)=="undefined"){
				g_data[key].prop={
					"size":"small",
					"level_name":"所有等级",
		    	    "level_id":""
	    	    };
				var newOption="<option value='' more_url='' level_name=''>请选择</option>",
					grouplen=technician_level!=""?technician_level.length:undefined,
					itempd=false;
				if(grouplen){	
					for(var i=0;i<grouplen; i++){
						newOption+="<option value='"+technician_level[i].level_id+"' more_url='"+technician_level[i].more_url+"' level_name='"+technician_level[i].level_name+"'>"+technician_level[i].level_name+"</option>";
					}
				}
				$(thisEdit).find("select[name='choicetech']").append(newOption);
        	} 
        
	     	/*icheck初始化*/
	        $(thisEdit).find("input").iCheck(main.iCheckOption);
	         
	        /*select初始化 以及选择分组操作*/
	        $(thisEdit).find("select").selectModify({
	        	onSelectChange:function(e,attrs){
	        		if($(e).attr("name")=="choicetech"){
	        			var level_id=attrs.value,
	        				level_name=attrs.level_name;
	        			if(level_id=="" || level_id==g_data[key].prop.level_id)return;				
						
						//改变人员显示方式
						var newPanelLi=$(main.getPanel("technician")).find(".tech-list ul").html(),	                        
	            			newEditLi=$(main.getEdit("technician")).find(".tech-list-show ul").html();
	            		$(thisPanel).find(".tech-list ul").html(newPanelLi);
	            		$(thisEdit).find(".tech-list-show ul").html(newEditLi);
						g_data[key].item_list=[];
						g_data[key].prop.level_id=level_id;
						g_data[key].prop.level_name=level_name;

	        		}
	        	}
	        });
     	      	
        	//选择服务人员
        	$(thisEdit).find(".choiceTech").unbind("click").click(function(){
				
				//此处应发AJAX请求，暂用模似
				if(g_data[key].prop.level_id==""){
					layer.msg("请先选择人员分组");
					return;
				}
				var nowItem = g_data[key].prop.level_id;
				
				main.technicianList(nowItem,key);			
				
				var technicianList=layer.open({
							type: 1,
							content: $('#technicianLayer'),
							offset: '10%',
							btn: ['确定','取消'], //按钮
							success: function(layero, index){
						        $(layero).checkAll();
						    },
							yes:function(){   //添加
								if($("#technicianLayer input[name='checkbox']").length>0){
															var newArray=[];							    
															$("#technicianLayer input[name='checkbox']").each(function(){
																if($(this).is(":checked")){
																	var shi=true,nelen=g_data[key].item_list.length;
																	if(nelen>0){
																		for(var ne=0;ne<nelen;ne++){
																			if(g_data[key].item_list[ne]==JSON.parse($(this).val()).id){
																				shi=false;
																			}
																		}
																	}
																	if(shi)newArray.push(JSON.parse($(this).val()));
																}
															});							
															if(newArray.length>0){
																var listmbE=$(thisEdit).find(".moban").html(),
																    listmbP=$(thisPanel).find(".moban").html();							    
																    
																for(var g=0;g<newArray.length;g++){
																		
																	var newobj={
																    		"id":newArray[g]['id'],
															    			"photo_url":newArray[g]['photo_url'],
															    			"nickname":newArray[g]['nickname'],
															    			"level":newArray[g]['level'],
															    			"sale":newArray[g]['sale']
																    	};
																	
																	//加入编辑模板
																	if($(thisEdit).find(".tech-list-show ul li[rel='yuan']").length>0){
																		$(thisEdit).find(".tech-list-show ul li[rel='yuan']").eq(0).find(".com-list-box-choice-pic").html("<img src='"+newobj.photo_url+"'>");
																		$(thisEdit).find(".tech-list-show ul li[rel='yuan']").eq(0).find(".c333").html(newobj.nickname);
																		$(thisEdit).find(".tech-list-show ul li[rel='yuan']").eq(0).find(".techSales .mt10").html(newobj.sale);
																		$(thisEdit).find(".tech-list-show ul li[rel='yuan']").eq(0).find(".techLevel .mt10").html(newobj.level);
																		$(thisEdit).find(".tech-list-show ul li[rel='yuan']").eq(0).attr("rel","");
																	}else{
																		var newStrE=main.formatTemplate(newobj,listmbE);
																		$(thisEdit).find(".tech-list-show ul").append(newStrE);
																	}
																	
																	//加入显示模板
																	if($(thisPanel).find(".tech-list ul li[rel='yuan']").length>0){
																		$(thisPanel).find(".tech-list ul li[rel='yuan']").eq(0).find("img").attr("src",newobj.photo_url);
																		$(thisPanel).find(".tech-list ul li[rel='yuan']").eq(0).find(".tech-title").html(newobj.nickname);
																		$(thisPanel).find(".tech-list ul li[rel='yuan']").eq(0).find(".tech-sale").html("已售："+newobj.sale);
																		$(thisPanel).find(".tech-list ul li[rel='yuan']").eq(0).attr("rel","");
																	}else{
																		var newStrP=main.formatTemplate(newobj,listmbP);
																		$(thisPanel).find(".tech-list ul").append(newStrP);
																	}
									
																	
																	//图片居中
																	g_data[key].item_list.push(newArray[g].id);
													        		_this.goodsImgCenter(thisPanel,thisEdit,key);
																	
																}
																$(thisEdit).find(".tech-list-show ul li[rel='yuan']").remove();
																$(thisPanel).find(".tech-list ul li[rel='yuan']").remove();
															}
								}
								layer.close(technicianList);
							},
							closeBtn:1,
							shadeClose: true,
							area: ['800px', '600px'] //宽高
				});

				
				
		    });
			
			//参数调整函数
			function propChange(thisPanel,thisEdit,key){
				
				//显示风格
				$(thisEdit).find("input[name='size']").on('ifChecked',function(){
					var sizeVal=$(this).val();
					$(thisPanel).find(".tech-list").attr("class","tech-list style-"+sizeVal);
					g_data[key].prop["size"]=sizeVal;
					_this.goodsImgCenter(thisPanel,thisEdit,key);
				});
				
				//显示价格
				$(thisEdit).find("input[name='showprice']:eq(0)").on('ifUnchecked',function(){
						$(thisPanel).find(".goods-price").html("");
						g_data[key].prop["showprice"]=0;
				});
				$(thisEdit).find("input[name='showprice']:eq(0)").on('ifChecked',function(){
						$(thisPanel).find(".goods-price").each(function(){
							$(this).html($(this).attr("yuandata"));
						});
						g_data[key].prop["showprice"]=1;					
				});				
				
			}		
			
		    //商品删除,上下移功能
		    function imgDelete(thisPanel,thisEdit,key){
				$(thisEdit).undelegate(".com-list-box-delete i",'click').delegate(".com-list-box-delete i",'click',function(){							
						  	var shunxu=$(thisEdit).find(".com-list-box-delete i").index(this),
						  		style_num=g_data[key].prop.size;						  		
							  	if($(thisPanel).find(".tech-list ul li").length<=1){							  		
							  			$(thisPanel).find(".tech-list ul li:eq(0)").attr("rel","yuan");
										$(thisPanel).find(".tech-list ul li:eq(0)").find("a").attr("href","");
										$(thisPanel).find(".tech-list ul li:eq(0)").find("img").attr("src",app.baseUrl+"/seller/images/shop_decorate/technician_default_img.png");
										$(thisPanel).find(".tech-list ul li:eq(0)").find(".tech-title").html("第1个服务人员");
										// $(thisPanel).find(".tech-list ul li:eq(0)").find("del").html("￥市场价");
										// $(thisPanel).find(".tech-list ul li:eq(0)").find(".tech-level1").html("￥返佣");
										$(thisPanel).find(".tech-list ul li:eq(0)").find(".tech-sale").html("已售:数量");
										
										//图片居中
										_this.goodsImgCenter(thisPanel,thisEdit,key);
							  	}else{
							  		$(thisPanel).find(".tech-list ul li:eq('"+shunxu+"')").remove();
							  	}

								if($(thisEdit).find(".tech-list-show ul li").length<=1){
										$(thisEdit).find(".tech-list-show ul li:eq(0)").attr("rel","yuan");
										$(thisEdit).find(".tech-list-show ul li:eq(0)").find(".com-list-box-choice-pic").html("<div class='default-text'><span>服务人</span><span>员图片</span></div>");
										$(thisEdit).find(".tech-list-show ul li:eq(0)").find(".c333").html("第1个服务人员");
										// $(thisEdit).find(".tech-list-show ul li:eq(0)").find(".goodsPricePrevious .mt10").html("0.0");
										// $(thisEdit).find(".tech-list-show ul li:eq(0)").find(".goodsPriceNow .mt10").html("0.0");
										$(thisEdit).find(".tech-list-show ul li:eq(0)").find(".techSales .mt10").html("0.0");
										$(thisEdit).find(".tech-list-show ul li:eq(0)").find(".techLevel .mt10").html("---");
								}else{
										$(this).parents("li").remove();
								}
							  	g_data[key].item_list.splice(shunxu,1);
				});				
				
			
				
				//上移一个位置				
				$(thisEdit).undelegate(".com-list-box-operate .arrow-up",'click').delegate(".com-list-box-operate .arrow-up",'click',function(){
					if($(this).parents("li").find(".com-list-box-choice-pic img").length<1) return;
				  	var index=$(thisEdit).find(".com-list-box-operate .arrow-up").index(this),
				  	    htmlE=$(this).parents("li").prop("outerHTML"),
				  	    htmlP=$(thisPanel).find(".tech-list ul li:eq("+index+")").prop("outerHTML");
				  	if(index>0){
					  	  $(thisEdit).find(".tech-list-show ul li:eq("+(index-1)+")").before(htmlE);
					  	  $(thisEdit).find(".tech-list-show ul li:eq("+(index+1)+")").remove();
					  	  $(thisPanel).find(".tech-list ul li:eq("+(index-1)+")").before(htmlP);
					  	  $(thisPanel).find(".tech-list ul li:eq("+(index+1)+")").remove();					  	  
					  	  main.exchange(g_data[key].item_list,(index-1),index);
					  	  console.log(g_data[key].item_list);
				  	}    
				});
				
				//下移一个位置
				$(thisEdit).undelegate(".com-list-box-operate .arrow-down",'click').delegate(".com-list-box-operate .arrow-down",'click',function(){
					if($(this).parents("li").find(".com-list-box-choice-pic img").length<1) return;
				  	var index=$(thisEdit).find(".com-list-box-operate .arrow-down").index(this),
				  		lilen=$(thisEdit).find(".com-list-box-operate .arrow-down").length,
				  	    htmlE=$(this).parents("li").prop("outerHTML"),
				  	    htmlP=$(thisPanel).find(".tech-list ul li:eq("+index+")").prop("outerHTML");
				  	if(index<(lilen-1)){
					  	  $(thisEdit).find(".tech-list-show ul li:eq("+(index+1)+")").after(htmlE);
					  	  $(thisEdit).find(".tech-list-show ul li:eq("+index+")").remove();
					  	  $(thisPanel).find(".tech-list ul li:eq("+(index+1)+")").after(htmlP);
					  	  $(thisPanel).find(".tech-list ul li:eq("+index+")").remove();					  	  
					  	  main.exchange(g_data[key].item_list,index,(index+1));
					  	  console.log(g_data[key].item_list);
				  	}    
				});	
		    }
		    
		    		    
		    propChange(thisPanel,thisEdit,key);
		    imgDelete(thisPanel,thisEdit,key);     	
     	
     	
     	
		    //调用主模板JS
		    main.saveClick();
		    main.moduleActive();
	        main.dragEdit();
        }   

	}
});